//
//  MineHelpCenterController.swift
//  HappyEat
//
//  Created by ctx2016 on 17/1/12.
//  Copyright © 2017年 陌路是我. All rights reserved.
//

import UIKit

class MineHelpCenterController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
    
    var dataArray:[[String:Any]] = [["content":"asdasdasdasdasda","title":"充值100元先送100元","selected":false], ["content":"asdasdasdasdasda","title":"充值100元先送100元","selected":false],
        ["content":"asdasdasdasdasda","title":"充值100元先送100元","selected":false],
        ["content":"asdasdasdasdasda","title":"充值100元先送100元","selected":false],
        ["content":"asdasdasdasdasda","title":"充值100元先送100元","selected":false],
        ["content":"asdasdasdasdasda","title":"充值100元先送100元","selected":false]]
    
    var modelArray:[HelpCenterModel] = [HelpCenterModel]()
    
    let cellId:String = "CELLID"
    
    let headerId:String = "HEADERID"
    
    var tableview:UITableView = UITableView.init(frame: .zero, style:.grouped)

    override func viewDidLoad() {
        super.viewDidLoad()
        self.createTableview()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.setNavi()
    }
}
extension MineHelpCenterController{
    //MARK:设置导航栏选项
    func setNavi(){
        self.navigationItem.title = "帮助中心"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black]
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "", imagStr: "ic_back", fontSize: 14, target: self, action: #selector(leftBarButtonAction))
    }
    //MARK:LeftBarButtonAction
    func leftBarButtonAction(){
        self.navigationController!.popViewController(animated: true)
    }
    func createTableview(){
        for item in self.dataArray{
            let model:HelpCenterModel = HelpCenterModel.init(dataDic: item)
            self.modelArray.append(model)
        }
        self.view.addSubview(self.tableview)
        self.tableview.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
            make.top.equalTo(0)
        }
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.register(HelpCenterCell.self , forCellReuseIdentifier: cellId)
        self.tableview.reloadData()
        self.tableview.separatorStyle = .none
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (self.modelArray[section].selected!) {
            return 1
        }else{
            return 0
        }
    }
    @objc(numberOfSectionsInTableView:) func numberOfSections(in tableView: UITableView) -> Int {
        return (self.modelArray.count) 
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        self.tableview.register(HelpHeaderView.self, forHeaderFooterViewReuseIdentifier: headerId+String(section))
        var headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerId+String(section))
        if headerView == nil{
            headerView = HelpHeaderView.init(reuseIdentifier: headerId+String(section))
        }
        (headerView as!HelpHeaderView).block = {
            (model) in
            self.modelArray[section] = model
            self.tableview.reloadData()
        }
        (headerView as!HelpHeaderView).model = self.modelArray[section]
        return headerView
    }
    @objc(tableView:cellForRowAtIndexPath:) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId)
        (cell as!HelpCenterCell).model = self.modelArray[indexPath.section]
        return cell!
    }
    @objc(tableView:heightForRowAtIndexPath:) func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UILabel.init().jqGetHeightWithstr(self.modelArray[indexPath.section].content, withfontSize: 14, withLineSpace: 0, withmaxWidth: k_Wide-32)+24
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
}

